<template>
    <div class="system-offer-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover" class="layout-padding-auto">

                <div class="table-search-container">
                    <el-form ref="tableSearchRef" :model="state.ruleForm" size="default" label-width="120px"
                        class="table-form">
                        <el-row>



                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                                <el-form-item label="图片类型">
                                    <el-select v-model="state.ruleForm.photoType" placeholder="请选择图片类型" clearable
                                        style="width: 100%">
                                        <!-- <el-option label="启动页" value="LAUNCH_PAGE"></el-option> -->
                                        <el-option label="Banner" value="BANNER"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                                <el-form-item label="语言类型">
                                    <el-select v-model="state.ruleForm.languageType" placeholder="请选择语言类型" clearable
                                        style="width: 100%">
                                        <!-- <el-option label="中文" value="1"></el-option> -->
                                        <el-option label="英文" :value="2"></el-option>
                                        <el-option label="中文繁体" :value="3"></el-option>
                                        <el-option label="越南语" :value="4"></el-option>
                                        <el-option label="日语" :value="5"></el-option>
                                        <el-option label="韩语" :value="6"></el-option>
                                        <el-option label="泰语" :value="7"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>


                            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                                <el-form-item class="table-form-btn" label-width="35px">
                                    <div>
                                        <el-button size="default" type="primary" @click="refresh()">查询
                                        </el-button>
                                        <el-button size="default" type="info" class="ml10" @click="handleResetSearch()"> 重置
                                        </el-button>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>





                <div class="bar_tools">
                    <el-button-group class="ml-4">
                        <el-button type="primary" size="default" @click="addItem()">添加图片</el-button>
                    </el-button-group>
                </div>

                <el-table size="default" :data="state.dataList" style="width: 100%">

                    <el-table-column align="center" label="排序">
                        <template #default="scope">
                            <span>{{ scope.row.sort }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="图片类型">
                        <template #default="scope">
                            <span>{{ scope.row.photoType }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="图片">
                        <template #default="scope">
                            <el-image style="width: 100px; height: 100px" :src="scope.row.imageUrl"
                                :preview-teleported="true" :preview-src-list="[scope.row.imageUrl]">
                            </el-image>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="语言">
                        <template #default="scope">
                            <el-tag v-if="scope.row.languageType == 2" effect="dark" type="warning">英文</el-tag>
                            <el-tag v-if="scope.row.languageType == 1" effect="dark" type="success">中文</el-tag>
                            <el-tag v-if="scope.row.languageType == 3" effect="dark" type="success">中文繁体</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="状态">
                        <template #default="scope">
                            <el-tag v-if="scope.row.status == 0" effect="dark" type="danger">禁用</el-tag>
                            <el-tag v-if="scope.row.status == 1" effect="dark" type="success">启用</el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column width="200" align="center" label="创建时间">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.createTime) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column width="200" align="center" label="更新时间">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.updateTime) }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="操作">
                        <template #default="scope">
                            <el-button-group size="small" class="btn-add">
                                <el-button size="small" type="primary"
                                    @click.native.prevent="editItem(scope.row)">编辑</el-button>
                                <el-button size="small" v-if="scope.row.status == 0" type="success"
                                    @click.native.prevent="operate(scope.row, 1)">启用</el-button>
                                <el-button size="small" v-if="scope.row.status == 1" type="danger"
                                    @click.native.prevent="operate(scope.row, 0)">禁用</el-button>
                            </el-button-group>
                        </template>
                    </el-table-column>


                </el-table>
            </el-card>

            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.page" background
                v-model:page-size="state.limit" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
            </el-pagination>
        </el-card>
        <Editor ref="EditorRef" @refresh="refresh"></Editor>
    </div>
</template>

<script setup lang="ts" name="systemMenu">

import { formatDate } from '/@/utils/formatTime';
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

import { useCommonApi } from '/@/api/common/index'


// 引入组件
const Editor = defineAsyncComponent(() => import('./dialog.vue'));
const EditorRef = ref();



const state = reactive({
    page: 1,
    limit: 10,
    total: 0,
    dataList: [] as any,
    ruleForm: {
        languageType: "",
        photoType: ""
    }
});








const editItem = (row: any) => {
    EditorRef.value.openDialog('edit', row);
}


const addItem = () => {
    EditorRef.value.openDialog('add');
}


const refresh = () => {
    getPhotoList();
}


// 修改状态操作
const operate = (item: any, status: any) => {
    ElMessageBox.confirm('此操作将启用/禁用图片, 是否继续?', '提示', {
        type: "warning",
        confirmButtonText: '确认',
        cancelButtonText: "取消"
    }).then(async () => {
        try {
            let obj = {
                id: item.id,
                status: status
            }
            await useCommonApi().updatePhotoStatus(obj);
            ElMessage.success("操作成功")
            refresh();
        } catch (error: any) {
            ElMessage.error(error.msg);
        }
    });
}

const handleResetSearch = () => {
    state.ruleForm = {
        languageType: "",
        photoType: ""
    };
    state.page = 1;
    state.limit = 10;
    getPhotoList();
}



const formatDate2 = (str: any) => {
    if (!str) return "-";
    return formatDate(new Date(str), "YYYY-mm-dd HH:MM")
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.limit = val;
    getPhotoList();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.page = val;
    getPhotoList();
};



const getPhotoList = async () => {
    let vo = {
        page: state.page,
        limit: state.limit
    } as any;

    if (state.ruleForm.photoType) vo.photoType = state.ruleForm.photoType.toString();
    if (state.ruleForm.languageType) vo.languageType = parseInt(state.ruleForm.languageType);

    let tempList = await useCommonApi().getPhotoList(vo);
    state.dataList = tempList.list;




    state.total = tempList.total;
}




// 页面加载时
onMounted(() => {
    getPhotoList();
});



</script>




<style scoped lang="scss">
.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.system-offer-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}


.table-search-container {
    display: flex;

    .table-form {
        flex: 1;

        .table-form-btn-toggle {
            white-space: nowrap;
            user-select: none;
            display: flex;
            align-items: center;
            color: var(--el-color-primary);
            margin-right: 15px;
        }
    }
}
</style>

